package com.southminority.ethnic.controller.coures;

import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.controller.coures.vo.CoursePageReqVo;
import com.southminority.ethnic.service.course.CourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southminority.ethnic.controller.coures.vo.CourseDetailVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import static com.southminority.ethnic.common.context.UserContext.getCurrentUserEmail;
import static com.southminority.ethnic.common.context.UserContext.getCurrentUserId;

@Slf4j
@Tag(name = "课程管理",description ="课程相关接口" )
@RestController
@RequestMapping("/api/course")
@CrossOrigin
public class CouresController {
    
    @Autowired
    private CourseService courseService;
    
    /**
     * 分页查询课程列表（包含教师信息）
     */
    @Operation(summary = "分页查询课程列表")
    @PostMapping("/page")
    public R<Page<CourseDetailVo>> getCoursePage(@Valid @RequestBody CoursePageReqVo reqVo) {
        try {
            Page<CourseDetailVo> result = courseService.getCoursePageWithTeacher(
                reqVo.getPageNum(), 
                reqVo.getPageSize(), 
                reqVo.getCourseType()
            );
            log.info("测试用户登录情况，id={},email={}",getCurrentUserId(),getCurrentUserEmail());
            return R.ok(result);
        } catch (Exception e) {
            log.error("分页查询课程列表失败", e);
            return R.fail("查询失败：" + e.getMessage());
        }
    }
}
